jsp动作<jsp:useBean>的问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 11:54:31
bean类:
package bean;

public class Test {
private int age=5;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
---------------
jsp代码:
<%@ page language="java" import="bean.Test;" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="age" class="bean.Test" scope="page" >

</jsp:useBean>
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" cont

问题1:
看看Doumentation,就知道了:The only real requirement for a JavaBean is that it has a zero argument constructor. 也就是说,在jsp中使用javaBean必须用无参构造器
在class Test中加一个constructor, public Test{age = 0;}

问题2:
The property is declared as a variable in a Bean and must have a corresponding getter method. 也就是说,property的名字,应该和你variable的名字一样的。
所以在<jsp:getProperty name="age" property="getAge"/> 中,应该改为property="age"。 因为你在class Test已经定义了private int age;

参考:SUN的JSP Specification
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm10.html

<jsp:getProperty name="age" property="getAge"/>

改为 <jsp:getProperty name="age" property="age"/>

<jsp:getProperty name="age" property="getAge"/>
这句,你仔细看看,在bean.test里面有